﻿
@{
    ViewBag.Title = "用户管理";
}

@section SideNav{ @Html.Partial("SideNavPartialView")}
@Styles.Render("~/Content/bootstrapplugincss")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrapplugin")

<style type="text/css">
    #userGrid td{
        text-align:center;
    }
</style>

<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span> @Html.ActionLink("首页", "Index","Home")</li>
    <li class="active">用户管理</li>
</ol>

<table id="userGrid"></table>

<script type="text/javascript">

    $(document).ready(function () {
        //表格开始
        $table = $("#userGrid");
        $table.bootstrapTable({
            showRefresh: true,
            showColumns: true,
            search: true,
            pagination: true,
            sidePagination: "server",
            pagelist:"[5, 10, 20, 30]",
            method: "post",
            url: "@Url.Action("FindPageJson","User")",
            columns:[
                { title: "ID", field: "UserID" },
                {
                    title: "角色", field: "RoleID"
                },                     
                { title: "用户名", field: "Username", formatter: function (value, row, index) {
                            return "<a href = '#' data-method = 'Modify' data-value=" + row.UserID + ">" + value + "  ( " + row.Name + ")</a>" }},
                { title: "性别", field: "Sex", formatter: function (value) { if (value == 0) return "男"; else if (value == 1) return "女"; else return "保密"; }},
                { title: "Email", field: "Email", visible:false },
                { title: "最后登录时间", field: "LastLoginTime" },
                { title: "最后登录IP", field: "LastLoginIP", visible:false },
                { title: "注册时间", field: "RegTime",visible:false },
                {title: "操作", field: "UserID", formatter: function(value){
                            return "<a class='btn btn-sm btn-danger' data-operation='deleteUser' data-value='" + value + "'>删除</a>" }}
            ]
            ,
            onLoadSuccess: function(){
                //删除按钮
                $("a[data-operation='deleteUser']").click(function(){
                                var id = $(this).attr("data-value");
                                BootstrapDialog.confirm("您确定删除" + $(this).parent().parent().find("td").eq(2).text() + "吗?", function(result){
                                    if(result){
                            $.post("@Url.Action("DeleteJson", "User")", { id: id}, function(data){
                                            if(data.Code == 1){
                                                BootstrapDialog.show({
                                                message: "删除用户成功",
                                        buttons:[{
                                            icon: "glyphicon glyphicon-ok",
                                            label: "确定",
                                            action: function(dialogItself){
                                                $table.bootstrapTable("refresh");
                                                            dialogItself.close();
                                                        }
                                                    }]
                                    });
                                            }
                                            else{
                                                BootstrapDialog.confirm(data.Message);
                                            }
                                        });
                                    }
                                });
                            });
                //删除按钮结束

                //修改用户信息
                $("a[data-method='Modify'").click(function () {
                    var id = $(this).attr("data-value");
                    var modifyDialog = new BootstrapDialog({
                        title: "<span class = 'glyphicon glyphicon-user'> 修改用户信息</span>",
                        message: function (dialog) {
                            $message = $("<div></div>");
                            var pageToLoad = dialog.getData('pageContent');
                            $message.load(pageToLoad);

                            return $message;
                        },
                        data: {
                            'pageContent' : '@Url.Action("Modify", "User")/' + id
                        },
                        buttons: [
                            {
                                label: "保存",
                                icon: "glyphicon glyphicon-saved",
                                action: function () {
                                    console.log($("form").attr("action"));
                                    $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                                        if (data.Code == 1) {
                                            BootstrapDialog.show({
                                                message: "用户信息修改成功",
                                                buttons: [{
                                                    label: "确定",
                                                    icon: "glypcicon glyphicon-ok",
                                                    action: function (dialogItself) {
                                                        $table.bootstrapTable("refresh");
                                                        dialogItself.close();
                                                        modifyDialog.close();
                                                    }
                                                }]
                                            });
                                        }
                                        else {
                                            BootstrapDialog.confirm(data.Message);
                                        }

                                    });
                                    $("form").validate();
                                }
                            },
                            {
                                label: "取消",
                                icon: "glyphicon glyphicon-remove",
                                action: function () { modifyDialog.close(); }
                            }
                        ]
                    });
                    modifyDialog.open();
                });
                //修改结束
                        }

        });
        //表格结束
    });
</script>